﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Hiro.ActivationPoints.Interfaces;
using Hiro.Interfaces;

namespace Hiro.ActivationPoints
{
    /// <summary>
    /// Represents the default implementation for a class that can determine which dependencies should be instantiated
    /// during a method call.
    /// </summary>
    public class ArgumentResolver : IArgumentResolver
    {
        /// <summary>
        /// Determines which dependency should be assigned to the particular parameter.
        /// </summary>
        /// <param name="parameter">The target parameter.</param>
        /// <param name="dependencyContainer">The dependency map that contains the list of dependencies for the current application.</param>
        /// <remarks>By default, this implementation will try to match the target parameter with an anonymous service that matches the parameter type.</remarks>
        /// <returns>An <see cref="IDependency"/> instance if the dependency can be found; otherwise, it will return <c>null</c>.</returns>
        public IDependency Resolve(ParameterInfo parameter, IDependencyContainer dependencyContainer)
        {
            var parameterType = parameter.ParameterType;
            var dependency = new Dependency(string.Empty, parameterType);

            // Use the anonymous dependency only if it exists
            if (!dependencyContainer.CanResolve(dependency))
                return null;

            return dependency;
        }
    }
}
